package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.dto.SalesRecordDTO;
import com.example.dto.SalesRecordQueryDTO;
import com.example.service.SalesRecordService;
import com.example.vo.SalesRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品售卖记录Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/sales")
public class SalesRecordController {

    @Autowired
    private SalesRecordService salesRecordService;

    /**
     * 添加售卖记录
     */
    @PostMapping("/record")
    public Result<Void> addSalesRecord(@RequestBody SalesRecordDTO salesRecordDTO) {
        try {
            log.info("添加售卖记录，参数：{}", salesRecordDTO);
            return salesRecordService.addSalesRecord(salesRecordDTO);
        } catch (Exception e) {
            log.error("添加售卖记录失败", e);
            return Result.error("添加失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询售卖记录
     */
    @GetMapping("/records")
    public Result<PageResult<SalesRecordVO>> getPageList(SalesRecordQueryDTO queryDTO) {
        try {
            log.info("查询售卖记录，参数：{}", queryDTO);
            return salesRecordService.getPageList(queryDTO);
        } catch (Exception e) {
            log.error("查询售卖记录失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询售卖记录详情
     */
    @GetMapping("/record/{id}")
    public Result<SalesRecordVO> getSalesRecordById(@PathVariable Long id) {
        try {
            log.info("查询售卖记录详情，ID：{}", id);
            return salesRecordService.getSalesRecordById(id);
        } catch (Exception e) {
            log.error("查询售卖记录详情失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 取消售卖记录
     */
    @PutMapping("/record/{id}/cancel")
    public Result<Void> cancelSalesRecord(@PathVariable Long id) {
        try {
            log.info("取消售卖记录，ID：{}", id);
            return salesRecordService.cancelSalesRecord(id);
        } catch (Exception e) {
            log.error("取消售卖记录失败", e);
            return Result.error("取消失败：" + e.getMessage());
        }
    }

    /**
     * 导出售卖记录
     */
    @GetMapping("/export")
    public Result<List<SalesRecordVO>> exportSalesRecords(SalesRecordQueryDTO queryDTO) {
        try {
            log.info("导出售卖记录，参数：{}", queryDTO);
            return salesRecordService.exportSalesRecords(queryDTO);
        } catch (Exception e) {
            log.error("导出售卖记录失败", e);
            return Result.error("导出失败：" + e.getMessage());
        }
    }

    /**
     * 根据商品ID查询售卖记录
     */
    @GetMapping("/records/product/{productId}")
    public Result<List<SalesRecordVO>> getSalesRecordsByProductId(@PathVariable Long productId) {
        try {
            log.info("根据商品ID查询售卖记录，商品ID：{}", productId);
            return salesRecordService.getSalesRecordsByProductId(productId);
        } catch (Exception e) {
            log.error("根据商品ID查询售卖记录失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取当前店长的售卖记录
     */
    @GetMapping("/my-records")
    public Result<List<SalesRecordVO>> getMySalesRecords() {
        try {
            log.info("获取我的售卖记录");
            return salesRecordService.getMySalesRecords();
        } catch (Exception e) {
            log.error("获取我的售卖记录失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }
} 